//
//  ViewController.m
//  08 - UItableVIew
//
//  Created by 韩旭 on 16/7/25.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"
#import "Model.h"
#import "HX_ Details.h"
#import "HX_AddStudent.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, HX_AddStudentDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@property (nonatomic, strong) NSMutableArray <NSString *>*arrName;
@property (nonatomic, strong) NSMutableArray <NSIndexPath *> *arrIndex;


@end

@implementation ViewController

- (void)reloadData {
    
}
- (NSMutableArray *)arrDataSource {

    if (_arrDataSource == nil) {
        self.arrDataSource = [NSMutableArray array];
    }
    return _arrDataSource;
}
// 实现协议方法
- (void)addName:(Model *)model {
    NSLog(@"%@", model);
    [_arrDataSource addObject:model];
    [_tableView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
  self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = @"通讯录";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPush:)];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    [self creatTableView];
}

- (void)addPush:(UIBarButtonItem *)bar {
    HX_AddStudent *add = [[HX_AddStudent alloc] init];
//    签订协议代理人
    add.delegate = self;
    [self.navigationController pushViewController:add animated:YES];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:YES animated:YES];
    if (editing) {
        NSLog(@"编辑");
    } else {
        [_arrDataSource removeObjectsInArray:_arrName];
        [_tableView deleteRowsAtIndexPaths:_arrIndex withRowAnimation:0];
        [_arrIndex removeAllObjects];
        [_arrName removeAllObjects];
        NSLog(@" 编辑完成");
    }
    
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDETH, HEIGHT) style:0];
     self.arrDataSource = [[Model groups] mutableCopy];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = COLORAUTO;
    [self.view addSubview:_tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrDataSource.count;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:3 reuseIdentifier:ID];
    }
    cell.textLabel.text = [[_arrDataSource objectAtIndex:indexPath.row] name];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    _tableView.separatorStyle = 0;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

// 5.提交
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_arrDataSource removeObjectAtIndex:indexPath.row];
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:0];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        [_arrDataSource insertObject:@"王璜" atIndex:indexPath.row];
        [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:0];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HX__Details *detail = [[HX__Details alloc] init];
    Model *model = self.arrDataSource[indexPath.row];
    detail.model = model;
    
    [self.navigationController pushViewController:detail animated:YES];
    
    [_arrName addObject:[_arrDataSource objectAtIndex:indexPath.row]];
    [_arrIndex addObject:indexPath];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_arrName removeObject:_arrDataSource[indexPath.row]];
    [_arrIndex removeObject:indexPath];
}


@end
